//
//  ShellMoreMilestoneView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellMoreMilestoneView.h"
#import "ShellMilestoneCell.h"
#import "LZBaseTableView.h"
#import "ShellHomeViewModel.h"
#import "ShellMilestoneModel.h"

@interface  ShellMoreMilestoneView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) ShellHomeViewModel *viewModel;

@end

@implementation ShellMoreMilestoneView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.cornerRadius = 6;
        self.backgroundColor = UIColorHex(0x08233F);
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    UILabel *titleLab = [UILabel lz_labelWithText:LZLocalizedString(@"里程碑") fontSize:k_textFont(14) color:UIColorHex(0x156097)];
    [self addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(22);
    }];
    
    
    [self addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-50);
    }];
    
    UIButton *nextBtn = [UIButton lz_buttonImageName:@"icon_next" backgroundImageName:@""];
    nextBtn.transform = CGAffineTransformMakeRotation(180 *M_PI / 180.0);
    [self addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(25);
    }];
    
    @weakify(self);
    [[nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [ShellMoreMilestoneView dismiss];
    }];
    

    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    
    [self.viewModel getStaticInfo:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.milestoneArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellMilestoneCell *milestoneCell = [tableView dequeueReusableCellWithIdentifier:@"ShellMilestoneCell"];
    milestoneCell.modelObject = self.viewModel.milestoneArray[indexPath.row];
    return milestoneCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   ShellMilestoneModel*milestoneModel = self.viewModel.milestoneArray[indexPath.row];
    return milestoneModel.celleHeight;
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColorHex(0x08233F);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ShellMilestoneCell class] forCellReuseIdentifier:@"ShellMilestoneCell"];
    }
    return _tableView;
}

- (ShellHomeViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellHomeViewModel alloc] init];
    }
    return _viewModel;
}

+ (void)show{
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    for (UIView * aView in window.subviews) {
        if (aView.tag  == 20191003){
            return;
        }
    }
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, K_SCREENHEIGHT)];
    contentView.tag = 20191003;
    contentView.backgroundColor = [UIColorHex(0x000000) colorWithAlphaComponent:0.5];
    [window addSubview:contentView];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
    [contentView addGestureRecognizer:tapGestureRecognizer];
    ShellMoreMilestoneView *milestoneView = [[ShellMoreMilestoneView alloc]initWithFrame:CGRectMake(15, (K_SCREENHEIGHT - 382)/2.0, K_SCREENWIDTH - 30, 382)];
    [contentView addSubview:milestoneView];
    
    [[tapGestureRecognizer rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [ShellMoreMilestoneView dismiss];
    }];
    
}
+ (void)dismiss{
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    for (UIView * aView in window.subviews) {
        if (aView.tag  == 20191003){
            [aView removeFromSuperview];
        }
    }
}

@end
